package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

//    @Autowired
//    private ApartmentInfoMapper mapper;


    @Autowired
    private ApartmentFacilityService facilityService;
    @Autowired
    private ApartmentFeeValueService feeValueService;
    @Autowired
    private ApartmentLabelService labelService;
    @Autowired
    private GraphInfoService graphInfoService;


    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;


//    @Autowired
//    private ApartmentFacilityMapper facilityMapper;

    @Override
    public Page<ApartmentItemVo> pageApartment(Page<ApartmentInfo> page, ApartmentQueryVo queryVo) {
        Page<ApartmentItemVo> page1 = baseMapper.selectPageApartment(page, queryVo);
        return page1;
    }

    @Override
    public void saveOrUpdateApartmentInfo(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId();
        //判断是否为空，非空删除，达到一个更新的目的
        if (id != null) {
            //删除公寓配套信息
            LambdaUpdateWrapper<ApartmentFacility> facilityWrapper = new LambdaUpdateWrapper<>();
            facilityWrapper.eq(ApartmentFacility::getApartmentId, id);
            facilityService.remove(facilityWrapper);

            //删除公寓杂费信息
            LambdaUpdateWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaUpdateWrapper<>();
            feeValueWrapper.eq(ApartmentFeeValue::getApartmentId, id);
            feeValueService.remove(feeValueWrapper);

            //删除公寓标签信息
            LambdaUpdateWrapper<ApartmentLabel> labelWrapper = new LambdaUpdateWrapper<>();
            labelWrapper.eq(ApartmentLabel::getApartmentId, id);
            labelService.remove(labelWrapper);

            //删除公寓图片信息
            LambdaUpdateWrapper<GraphInfo> graphInfoWrapper = new LambdaUpdateWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getItemId, id);
            graphInfoService.remove(graphInfoWrapper);
        }
        saveOrUpdate(apartmentSubmitVo);

        //获取到公寓配套id的list集合 看关联表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        //spring操作集合的工具类 空为真
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<ApartmentFacility> apartmentFacilities = new ArrayList<>();

            //遍历facilityInfoIds集合，通过公寓id和这俩id的关联表取出配套的id
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(facilityInfoId).build();
                apartmentFacilities.add(apartmentFacility);
            }
            facilityService.saveBatch(apartmentFacilities);

        }

        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();

            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
                apartmentFeeValues.add(apartmentFeeValue);
            }
            feeValueService.saveBatch(apartmentFeeValues);

        }

        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            //创建了一个关联对象的集合 关联对象对应apartment_label
            List<ApartmentLabel> apartmentLabels = new ArrayList<>();

            //遍历这个labelId的集合
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(labelId).build();
                apartmentLabels.add(apartmentLabel);
            }
            labelService.saveBatch(apartmentLabels);
        }

        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setUrl(graphVo.getUrl());

                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVOById(Long id) {
        ApartmentInfo apartmentInfo = this.getById(id);

        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoListByApartmentId(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoListByApartmentId(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVOListByApartmentId(id);
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVOByApartmentIdAndItemTypeId(id, ItemType.APARTMENT);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //借助工具类，将公寓信息的属性复制给apartmentDetailVo
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setGraphVoList(graphVoList);

        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentInfoById(Long id) {
        LambdaQueryWrapper<RoomInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomInfo::getApartmentId, id);
        Long roomCount = roomInfoMapper.selectCount(wrapper);

        if (roomCount == 0) {
            throw new LeaseException(ResultCodeEnum.APARTMENT_HAS_ROOM);
        }
        this.removeById(id);
        //删除公寓配套信息
        LambdaUpdateWrapper<ApartmentFacility> facilityWrapper = new LambdaUpdateWrapper<>();
        facilityWrapper.eq(ApartmentFacility::getApartmentId, id);
        facilityService.remove(facilityWrapper);

        //删除公寓杂费信息
        LambdaUpdateWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaUpdateWrapper<>();
        feeValueWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        feeValueService.remove(feeValueWrapper);

        //删除公寓标签信息
        LambdaUpdateWrapper<ApartmentLabel> labelWrapper = new LambdaUpdateWrapper<>();
        labelWrapper.eq(ApartmentLabel::getApartmentId, id);
        labelService.remove(labelWrapper);

        //删除公寓图片信息
        LambdaUpdateWrapper<GraphInfo> graphInfoWrapper = new LambdaUpdateWrapper<>();
        graphInfoWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoWrapper);
    }


}




